package com.learn.designPatterns.behavioral.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * @classname:
 * @Description:
 * @author: yfpu
 * @Date: 2018-10-21 14:11
 */
public class WeatherData implements Subject{

    private List<Observer> observerList;

    private float temperature;
    private float humidity;
    private float pressure;

    public WeatherData() {
        observerList = new ArrayList<>();
    }

    public void setMeasurements(float temperature, float humidity, float pressure) {
        this.temperature = temperature;
        this.humidity = humidity;
        this.pressure = pressure;
        notifyObserver();
    }

    @Override
    public void resisterObserver(Observer observer) {
        observerList.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        int i = observerList.indexOf(observer);
        if (i > 0)
            observerList.remove(i);
    }

    @Override
    public void notifyObserver() {
        for (Observer observer : observerList) {
            observer.update(temperature, humidity, pressure);
        }
    }
}
